package els;

import enums.EnumsColumsType;
import util.H3ElsEntity;
import util.RelaEntity;
import util.RelaTable;
import util.SearchEnums;

import java.util.List;

/**
 * @ClassName:
 * @Description:
 * @author: YuanWei
 * @date: 2019/01/29 17:04
 * @version: V1.0
 */
public class ElsBuildStr {


    public static String buildS(H3ElsEntity h3ElsEntity){
        StringBuffer buf = new StringBuffer();
        buf.append("{\n" +
                "\t\"tableName\":\"").append(h3ElsEntity.getTableName()).append("\",\n" +//表名
                "\t\"tbNameRegex\":\"").append(h3ElsEntity.getTableName()).append("_(\\\\d{1})\", \n" +//表名正则表达式
                "\t\"tableMergeRule\":[\"ds(\\\\d{1}).").append(h3ElsEntity.getTableName()).append("_(\\\\d{1})\"], \n" +//数据合并正则表达式
                "\t\"elsTypeName\":\"_doc\",\n" +//elst中的typeName
                "\t\"relationType\":1, \n" +//关联表类型，1单表，2多表
                "\t\"unionPrimaryName\":[").append(h3ElsEntity.getTableNames()).append("],\n" +//关联主键
                "\t\"tableInfoRelas\":\n" +
                "\t\t[\n" );
        List<RelaEntity> l = h3ElsEntity.getRelaEntityList();
        for (RelaEntity r :l){
            buf.append("\t\t\t{\n" +
                    "\t\t\t\t\"tableName\":\"").append(r.getTableName()).append("\",\n" +
                    "\t\t\t\t\"relationType\":").append(r.getRelationType()).append(", \n" +//和主表关联关系 1指一对一，2一堆n
                    "\t\t\t\t\"unionPrimaryName\":[").append(r.getColumnNames()).append(",\"id\"],\n" +//从表关联主键
                    "\t\t\t\t\"columnInfos\":\n" +// 采集字段集（可以为空）
                    "\t\t\t\t[\n" +
                    "\t\t\t\t]\n" +
                    "\t\t\t},\n");
        }
        buf.setLength(buf.length()-2);
        buf.append("\n");
        buf.append("\t],\n" +
                "\t\"columnInfos\":\n" +
                "\t[\n" +
                "\t]\n" +
                "}");
        return buf.toString();
    }
    public static String budildStr(List<H3ElsEntity> list){
        StringBuffer buf = new StringBuffer();

        buf.append("{\n" +
                "\n" +
                "\"tableName\":\"").append(list.get(0).getTableName()).append("\",\n" +//表名
//                "\"listQueryParas\":{\"cpCode\":\"cp_code\"},\n" +//前端调用数据列表查找接口的参数转换
                "\"entityListColumns\":\n" +
                "[\n    ");
        for (H3ElsEntity tab : list){
            String type = tab.getEntityColumnType();
            if(type.equals(EnumsColumsType.nomal.getId())){
                String str = buildNormalStr(tab);
                buf.append(str);
                buf.append(",\n");
            }else if(type.equals(EnumsColumsType.search.getId())){
                String str = buildSearchStr(tab);
                buf.append(str);
                buf.append(",\n");
            }else if(type.equals(EnumsColumsType.en.getId())){
                String str = buildEnStr(tab);
                buf.append(str);
                buf.append(",\n");
            }else if(type.equals(EnumsColumsType.group.getId())){
                String str = buildGroupStr(tab);
                buf.append(str);
                buf.append(",\n");
            }else if(type.equals(EnumsColumsType.time.getId())){
                String str = buildTimeStr(tab);
                buf.append(str);
                buf.append(",\n");
            }else if(type.equals(EnumsColumsType.rela.getId())){
                String str = buildRela(tab);
                buf.append(str);
                buf.append(",\n");
            }
        }
        buf.setLength(buf.length()-2);
        buf.append("\n");
        buf.append("\n]\n" +
                "}");
//        System.out.println(buf.toString());
        return buf.toString();
    }

    /**
     * 普通列
     * @return
     */
    public static String buildNormalStr(H3ElsEntity tab){
        StringBuffer buf = new StringBuffer();
        buf.append("{\n" +//最简单列
//                "\t\t\"tableId\":").append(tab.getTableId()).append(",\n" +//数据库中的表名
                "\t\t\"tableName\":\"").append(tab.getTableName()).append("\",\n" +//表名
                "\t\t\"columnTitle\":\"").append(tab.getColumnTitle()).append("\",\n" +//列表header展示名（可以为空）
//                "\t\t\"columnId\":\"").append(tab.getColumnId()).append("\",\n" +//数据库中的字段id
                "\t\t\"columnKey\":\"").append(tab.getColumnKey()).append("\",\n" +//数据库中的字段名
                "\t\t\"columnHidden\":").append(tab.getColumnHidden()).append(", \n" +//是否隐藏
                "\t\t\"orderinfo\":").append(tab.getOrder()).append(",\n" +//字段排序
                "\t\t\"entityColumnType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"columnTypeName\":\"input\",\n" +//列表中的搜索展示形式 input,select
                "\t\t\t\"columnTypePlaceholder\":\"").append(tab.getColumnTitle()).append("\",\n" +//提示文字
                "\t\t\t\"columnTypeDisabled\":").append(tab.getColumnTypeDisabled()).append("\n" +//该列是否禁用搜索，0不禁用，1禁用
                "\t\t},\n" +
                "\t\t\"entityColumnDataType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"dataTypeName\":\"\"\n" +
                "\t\t}\n" +
                "\t}");
        return buf.toString();
    }

    /**
     * 搜索
     * @param tab
     * @return
     */
    public static String buildSearchStr(H3ElsEntity tab) {
        StringBuffer buf = new StringBuffer();
        buf.append("{\n" +//字段有搜索
//                "\t\t\"tableId\":").append(tab.getTableId()).append(",\n" +//数据库中的表名
                "\t\t\"tableName\":\"").append(tab.getTableName()).append("\",\n" +//数据库中的表名
                "\t\t\"columnTitle\":\"").append(tab.getColumnTitle()).append("\",\n" +//列表header展示名（可以为空）
//                "\t\t\"columnId\":\"").append(tab.getColumnId()).append("\",\n" +//数据库中的字段id
                "\t\t\"columnKey\":\"").append(tab.getColumnKey()).append("\",\n" +//数据库中的字段名
                "\t\t\"columnHidden\":").append(tab.getColumnHidden()).append(", \n" +//是否隐藏
                "\t\t\"orderinfo\":").append(tab.getOrder()).append(",\n" +//字段排序
                "\t\t\"entityColumnType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"columnTypeName\":\"select\",\n" +//类型为下拉
                "\t\t\t\"columnTypePlaceholder\":\"").append(tab.getColumnTitle()).append("\",\n" +//提示文字
                "\t\t\t\"entityColumnTypeData\":\n" +//下拉数据源（动态查找）
                "\t\t\t{\n" +
//                "\t\t\t\t\"typeDataTableId\":").append(tab.getRelationTableId()).append(",\n" +
                "\t\t\t\t \"typeDataTableName\":\"").append(tab.getRelationTableName()).append("\", \n" +//关联表
                "\t\t\t\t\"typeDataMethod\":\"POST\",\n" +
                "\t\t\t\t\"typeDataUrl\":\"\",\n" +
                "\t\t\t\t\"typeDataPara\":\"\",\n" +
//                "\t\t\t\t\"typeDataRelaColumnId\":").append(tab.getRelationColumnId()).append(",\n" +
                "\t\t\t\t\"typeDataSearchColumnName\":\"").append(tab.getRelationShowColumnName()).append("\",\n" +//搜索及展示的字段名()
//                "\t\t\t\t\"typeDataSearchColumnid\":").append(tab.getRelationShowColumnId()).append(",\t\t\n" +
                "\t\t\t\t\"typeDataRelaColumnName\":\"").append(tab.getRelationColumnName()).append("\"\n" +//关联字段名
                "\t\t\t}\n" +
                "\t\t},\n" +
                "\t\t\"entityColumnDataType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"dataTypeName\":\"\"\n" +
                "\t\t}\n" +
                "\t}");
        return buf.toString();
    }
    /**
     * 枚举
     * @param tab
     * @return
     */
    public static String buildEnStr(H3ElsEntity tab) {
        StringBuffer buf = new StringBuffer();
        buf.append("\t{\n" +//枚举展示
//                "\t\t\"tableId\":").append(tab.getTableId()).append(",\n" +//数据库中的表名
                "\t\t\"tableName\":\"").append(tab.getTableName()).append("\",\n" +//数据库中的表名
                "\t\t\"columnTitle\":\"").append(tab.getColumnTitle()).append("\",\n" +//列表header展示名（可以为空）
//                "\t\t\"columnId\":\"").append(tab.getColumnId()).append("\",\n" +//数据库中的字段id
                "\t\t\"columnKey\":\"").append(tab.getColumnKey()).append("\",\n" +//数据库中的字段名
                "\t\t\"columnHidden\":").append(tab.getColumnHidden()).append(", \n" +//是否隐藏
                "\t\t\"orderinfo\":").append(tab.getOrder()).append(",\n" +//字段排序
                "\t\t\"entityColumnType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"columnTypeName\":\"select\",\n" +
                "\t\t\t\"columnTypePlaceholder\":\"").append(tab.getColumnTitle()).append("\",\n" +
                "\t\t\t\"entityColumnTypeSets\":\n" +//搜索枚举值
                "\t\t\t[\n" );
        List<SearchEnums> searchEnumsList = tab.getSearchEnumsList();
        for (SearchEnums searchEnums :searchEnumsList) {
            buf.append("\t\t\t\t{\n" +
                    "\t\t\t\t\t\"typeSetValue\":\"").append(searchEnums.getTypeSetValue()).append("\",\n" +
                    "\t\t\t\t\t\"typeSetDisabled\":0,\n" +//0不弃用1弃用，
                    "\t\t\t\t\t\"typeSetLabel\":\"").append(searchEnums.getTypeSetLabel()).append("\"\n" +
                    "\t\t\t\t},\n");
        }
        buf.setLength(buf.length()-2);
        buf.append("\n");
        buf.append("\t\t\t]\n" +
                "\t\t},\n" +
                "\t\t\"entityColumnDataType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"dataTypeName\":\"\",\n" +
                "\t\t\t\"entityColumnDataTypeSets\":\n" +//展示枚举值
                "\t\t\t[\n" );
        for (SearchEnums searchEnums :searchEnumsList) {
            buf.append("\t\t\t\t{\n" +
                    "\t\t\t\t\t\"dataSetValue\":\"").append(searchEnums.getTypeSetValue()).append("\",\n" +
                    "\t\t\t\t\t\"dataSetLabel\":\"").append(searchEnums.getTypeSetLabel()).append("\"\n" +
                    "\t\t\t\t},\n");
        }
        buf.setLength(buf.length()-2);
        buf.append("\n");
        buf.append("\t\t\t]\n" +
                "\t\t}\n" +
                "\t}");
        return buf.toString();
    }
    /**
     * 范围
     * @param tab
     * @return
     */
    public static String buildGroupStr(H3ElsEntity tab) {
        StringBuffer buf = new StringBuffer();
        buf.append("\t{\n" +//范围查找
//                "\t\t\"tableId\":").append(tab.getTableId()).append(",\n" +//数据库中的表名
                "\t\t\"tableName\":\"").append(tab.getTableName()).append("\",\n" +//数据库中的表名
                "\t\t\"columnTitle\":\"").append(tab.getColumnTitle()).append("\",\n" +//列表header展示名（可以为空）
//                "\t\t\"columnId\":\"").append(tab.getColumnId()).append("\",\n" +//数据库中的字段id
                "\t\t\"columnKey\":\"").append(tab.getColumnKey()).append("\",\n" +//数据库中的字段名
                "\t\t\"columnHidden\":").append(tab.getColumnHidden()).append(", \n" +//是否隐藏
                "\t\t\"orderinfo\":").append(tab.getOrder()).append(",\n" +//字段排序
                "\t\t\"entityColumnType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"columnTypeName\":\"select\",\n" +
                "\t\t\t\"columnTypePlaceholder\":\"").append(tab.getColumnTitle()).append("\",\n" +
                "\t\t\t\"columnTypeExtend\":\"{3:true}\",\n" +//扩展字段（3，范围搜索对应值true）
                "\t\t\t\"entityColumnTypeSets\":\n" +//搜索枚举值
                "\t\t\t[\n" );
        List<SearchEnums> searchEnumsList = tab.getSearchEnumsList();
        for (SearchEnums searchEnums :searchEnumsList) {
            buf.append("\t\t\t\t{\n" +
                    "\t\t\t\t\t\"typeSetValue\":\"").append(searchEnums.getTypeSetValue()).append("\",\n" +
                    "\t\t\t\t\t\"typeSetDisabled\":0,\n" +//0不弃用1弃用，
                    "\t\t\t\t\t\"typeSetLabel\":\"").append(searchEnums.getTypeSetDisabled()).append("\"\n" +
                    "\t\t\t\t},\n");
        }
        buf.setLength(buf.length()-1);
        buf.append("\t\t\t]\n" +
                "\t\t},\n" +
                "\t\t\"entityColumnDataType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"dataTypeName\":\"\"\n" +
                "\t\t}\n" +
                "\t}");
        return buf.toString();
    }
    /**
     * 时间
     * @param tab
     * @return
     */
    public static String buildTimeStr(H3ElsEntity tab) {
        StringBuffer buf = new StringBuffer();
        buf.append("\t{\n" +//时间
//                "\t\t\"tableId\":").append(tab.getTableId()).append(",\n" +//数据库中的表名
                "\t\t\"tableName\":\"").append(tab.getTableName()).append("\",\n" +//数据库中的表名
                "\t\t\"columnTitle\":\"").append(tab.getColumnTitle()).append("\",\n" +//列表header展示名（可以为空）
//                "\t\t\"columnId\":\"").append(tab.getColumnId()).append("\",\n" +//数据库中的字段id
                "\t\t\"columnKey\":\"").append(tab.getColumnKey()).append("\",\n" +//数据库中的字段名
                "\t\t\"columnHidden\":").append(tab.getColumnHidden()).append(", \n" +//是否隐藏
                "\t\t\"orderinfo\":").append(tab.getOrder()).append(",\n" +//字段排序
                "\t\t\"entityColumnType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"columnTypeName\":\"time\",\n" +//时间类型
                "\t\t\t\"columnTypePlaceholder\":\"").append(tab.getColumnTitle()).append("\",\n" +
                "\t\t\t\"columnTypeExtend\":\"{5:\\\"daterange\\\"}\"\n" +//范围时间
                "\t\t},\n" +
                "\t\t\"entityColumnDataType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"dataTypeName\":\"\",\n" +
                "\t\t\t\"dateTypeResultType\":5" +
                "\t\t}\n" +
                "\t}");
        return buf.toString();
    }

    /**
     * 关联表额外字段展示
     * @param tab
     * @return
     */
    public static String buildRela(H3ElsEntity tab) {
        StringBuffer buf = new StringBuffer();
        buf.append("\t{\n" +//关联表额外字段展示
//                "\t\t\"tableId\":").append(tab.getTableId()).append(",\n" +//数据库中的表名
                "\t\t\"tableName\":\"").append(tab.getTableName()).append("\",\n" +//数据库中的表名
                "\t\t\"columnTitle\":\"").append(tab.getColumnTitle()).append("\",\n" +//列表header展示名（可以为空）
//                "\t\t\"columnId\":\"").append(tab.getColumnId()).append("\",\n" +//数据库中的字段id
                "\t\t\"columnKey\":\"").append(tab.getColumnKey()).append("\",\n" +//数据库中的字段名
                "\t\t\"columnHidden\":").append(tab.getColumnHidden()).append(", \n" +//是否隐藏
                "\t\t\"orderinfo\":").append(tab.getOrder()).append(",\n" +//字段排序
                "\t\t\"entityColumnType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"columnTypeName\":\"select\",\n" +
                "\t\t\t\"columnTypePlaceholder\":\"").append(tab.getColumnTitle()).append("\",\n" +
                "\t\t\t\"entityColumnTypeData\":\n" +
                "\t\t\t{\n" +
//                "\t\t\t\t\"typeDataTableId\":").append(tab.getRelationTableId()).append(",\n" +
                "\t\t\t\t\"typeDataTableName\":\"").append(tab.getRelationTableName()).append("\", \n" +//关联表
                "\t\t\t\t\"typeDataMethod\":\"POST\",\n" +
                "\t\t\t\t\"typeDataUrl\":\"\",\n" +
                "\t\t\t\t\"typeDataPara\":\"\",\n" +
//                "\t\t\t\t\"typeDataRelaColumnId\":").append(tab.getRelationColumnId()).append(",\n" +
                "\t\t\t\t\"typeDataSearchColumnName\":\"").append(tab.getRelationShowColumnName()).append("\",\n" +//搜索及展示的字段名()
//                "\t\t\t\t\"typeDataSearchColumnid\":").append(tab.getRelationShowColumnId()).append(",\t\t\n" +
                "\t\t\t\t\"typeDataRelaColumnName\":\"").append(tab.getRelationColumnName()).append("\"\n" +//关联字段名
                "\t\t\t}\n"+
                "\t\t},\n");

        buf.append("\t\"relaEntitycolumns\":[\n");
        List<RelaTable> relaList =tab.getRelaTableList();
        boolean f = false;
        for (RelaTable r : relaList){
            buf.append("\t\t\t{\n" +
//                    "\t\t\t\t\t\"tableId\":").append(tab.getRelationTableId()).append(",\n" +
                    "\t\t\t\t\"tableName\":\"").append(tab.getRelationTableName()).append("\",\n" +
                    "\t\t\t\t\"columnTitle\":\"").append(r.getColumnTypePlaceholder()).append("\",\n" +
//                    "\t\t\t\t\t\"columnId\":\"").append(r.getColumnId()).append("\",\n" +
                    "\t\t\t\t\"columnKey\":\"").append(r.getColumnKey()).append("\",\n" +
                    "\t\t\t\t\"columnHidden\":0, \n" +//是否隐藏
                    "\t\t\t\t\"orderinfo\":").append(r.getOrder()).append(",\n" +
                    "\t\t\t\t\"entityColumnType\":\n" +
                    "\t\t\t\t{\n" +
                    "\t\t\t\t\t\"columnTypeName\":\"").append(r.getColumnTypeName()).append("\",\n" +
                    "\t\t\t\t\t\"columnTypePlaceholder\":\"").append(r.getColumnTypePlaceholder()).append("\",\n" +
                    "\t\t\t\t\t\"columnTypeDisabled\":1\n" +
                    "\t\t\t\t},\n" +
                    "\t\t\t\t\"entityColumnDataType\":\n" +
                    "\t\t\t\t{\n" +
                    "\t\t\t\t\t\"dataTypeName\":\"\"\n" +
                    "\t\t\t\t}\n" +
                    "\t\t\t},\n");
            f = true;
        }
        if(f){
            buf.setLength(buf.length()-2);
            buf.append("\n");
        }

        buf.append("\t\t],\n" +
                "\t\t\"entityColumnDataType\":\n" +
                "\t\t{\n" +
                "\t\t\t\"dataTypeName\":\"\"\n" +
                "\t\t}\n" +
                "\t}\n");
        return buf.toString();
    }
}
